<?php
namespace app\api\controller;

use app\api\model\Merchant;
use app\api\model\User;

class Teams extends Base
{
    protected $autoNewModel = false;// 是否自动实例化当前模型
    protected $autoValidateCheck = false;// 是否开启自动验证

    public function information(){
//        background: '',
//        infos: {
//            avatar: '', nickname: '', rows: [], level_name: '', idents: [],
//            qr: {enable: true, text: '', alias: 'none', params: {}}
//        },
//        search: {enable: false, placeholder: '', button: ''},
//        ad: false,
//        invite: {enable: false, title: '', nickname: '', mobile: ''},
//        counts: {enable: false,
//            lists: [{name: '', count: '', type: ''}]
//        },
//        lists: {enable: false, title: '',
//            filters: {type: '', search_keyword: '', page: 1}
//        }
        $userId = request()->UserModel->id;
        $infos = User::with(['userLevel', 'userVirtualLevel'])->where('id', $userId)->find();
        $parentInfos = User::where('id', $infos['pid'])->find();
        $inviteStatus = true;
        if(empty($parentInfos)){
            $parentInfos['id'] = '';
            $parentInfos['phone'] = '';
            $inviteStatus = false;
        }
        $count = User::where('pid', $userId)->count();
        $str = '%|' . $userId . '|%';
        $teams_merchant_count = User::where('status', 'on')
            ->whereLike('parent_ids', $str)
            ->where('merchant_status', 'on')
            ->count();

        $is_merchant = false;
        $merchant_score = 0;
        if($merchant_info = Merchant::where('user_id', $userId)->where('status', 'on')->find())
        {
            $is_merchant = true;
            $merchant_score = bcdiv_amount($merchant_info['score'], \app\model\Base::$hundred);
        }
        //整理数据
        $data = [
            'main_title'=>'门店管理',
            'background'=>get_oss_url('/static/teams-bg.png'),
            'infos'=>[
                'avatar'=>get_oss_url('/static/logo.png'),
                'nickname'=>$infos['phone'],
                'rows'=>[
                    'ID：' . $infos['id'],
                    '手机号码：' . $infos['phone'],
                    '邀请码：' . $infos['invite_code'],
                ],
                'level_name'=>$infos['virtual_level_name'],
                'idents'=>[],
                'qr'=>[
                    'enable'=>true,
                    'text'=>'',
                    'alias'=>'none',
                    'params'=>[],
                ],
            ],
            'search'=>[
                'enable'=>false,
                'placeholder'=>'',
                'button'=>'',
            ],
            'ad'=>false,
            'invite'=>[
                'enable'=>$inviteStatus,
                'title'=>'我的邀请人',
                'nickname'=>'ID：' . $parentInfos['id'],
                'mobile'=>'',//$parentInfos['phone'],
            ],
            'counts'=>[
                'enable'=>true,
                'lists'=>[
                    [
                        'enable'=>true,
                        'name'=>'团队数量',
                        'count'=>$count,
                        'type'=>'',
                        'alias'=>'',//'teams.size_zone',
                        'params'=>[],
                        'action'=>''
                    ],
                    [
                        'enable'=>true,
                        'name'=>'直推数量',
                        'count'=>$count,
                        'type'=>'',
                        'alias'=>'',//'teams.size_zone',
                        'params'=>[],
                        'action'=>''
                    ],
                    [
                        'enable'=>true,
                        'name'=>'加盟店数量',
                        'count'=>$teams_merchant_count,
                        'type'=>'',
                        'alias'=>'merchant.franchise',
                        'params'=>[],
                        'action'=>''
                    ],
                    [
                        'enable'=>$is_merchant,
                        'name'=>'门店积分',
                        'count'=>$merchant_score,
                        'type'=>'',
                        'alias'=>'merchant.index',
                        'params'=>[],
                        'action'=>''
                    ],
                ],
            ],
            'lists'=>[
                'enable'=>true,
                'title'=>'',
                'filters'=>[
                    'type'=>'',
                    'search_keyword'=>'',
                    'page'=>1,
                ]
            ]
        ];
        //响应数据
        return showSuccess($data);
    }

    public function lists(){
        $userId = request()->UserModel->id;
        $lists = User::where('pid', $userId)
            ->append(['level_name', 'values'])
            ->select();
        //整理数据
        $data = [
            'lists'=>$lists
        ];
        //响应数据
        return showSuccess($data);
    }

    public function sizeZone()
    {
        $user_id = request()->UserModel->id;
        $infos = User::where('id', $user_id)->find();

        $small_zone_performance = 0;
        $big_zone_performance = 0;
        $big_zone_user_id = '暂无';
        if($big_zone_user = User::field('id, pid, status, spending+performance as total_performance')
            ->where('status', 'on')
            ->where('pid', $user_id)
            ->order('total_performance DESC')
            ->find()){

            //如果有分支
            //剩余团队业绩
            $small_zone_performance = $infos['performance'] - $big_zone_user['total_performance'];
            $big_zone_performance = $big_zone_user['total_performance'];
            $big_zone_user_id = $big_zone_user['id'];
        }
        //整理数据
        $data = [
            'main_title'=>'我的店铺详情',
//            'background'=>get_oss_url('/static/teams-bg.png'),
//            'infos'=>[
//                'avatar'=>get_oss_url('/static/logo.png'),
//                'nickname'=>$infos['phone'],
//                'rows'=>[
//                    'ID：' . $infos['id'],
//                    '手机号码：' . $infos['phone'],
//                    '邀请码：' . $infos['invite_code'],
//                ],
//                'level_name'=>'普通会员',
//                'idents'=>[],
//                'qr'=>[
//                    'enable'=>true,
//                    'text'=>'',
//                    'alias'=>'none',
//                    'params'=>[],
//                ],
//            ],
            'teams'=> [
                'enable'=>true,
                'performance'=>[
                    'name'=>'销售业绩(元)',
                    'text'=>bcdiv_amount($infos['performance']),
                ],
                'big_zone'=>[
                    'name'=>'大区销售业绩(元)',
                    'text'=>bcdiv_amount($big_zone_performance),
                ],
                'big_zone_user'=>[
                    'name'=>'大区销售员ID',
                    'text'=>$big_zone_user_id,
                ],
                'small_zone'=>[
                    'name'=>'小区销售业绩(元)',
                    'text'=>bcdiv_amount($small_zone_performance),
                ],
            ],
        ];
        //响应数据
        return showSuccess($data);
    }

    public function information2()
    {
        //整理数据
        $data = [
//            'logo'=> get_oss_url('/msyx/system/oauth/bg.png'),
            'main_title'=> '我的团队',
            'title'=> '众益 ',
            'description'=>'请使用手机号码进行登录',
            'wechat'=>[
                'enable'=> false, 'title'=> '', 'alias'=> '', 'show'=> false, 'tip'=> ''
            ],
            'third_platform'=> [
                'enable'=> false,
                'providers'=> [
                    [
                        'alias'=> 'sinaweibo',
                        'logo'=> ''
                    ],
                    [
                        'alias'=> 'weixin',
                        'logo'=> ''
                    ],
                    [
                        'alias'=> 'qq',
                        'logo'=> ''
                    ]
                ]
            ],
        ];
        return showSuccess($data);
    }
}